(function ($) {
    var defaults = {
        pageSize: 8,              //默认总页数
        pageIndex: 1,             //默认起始页
        color: '#000',            //默认未点击时页码颜色
        colorC: '#fff',           //默认点击后页码颜色
        background: '#eee',       //默认未点击时页码背景色
        backgroundC: '#F7836D'    //默认点击后页码背景色
    };
    function Pager(options) {
        this.options = options;
        this.creatCount();
        if (this.options.pageIndex < 1) {
            this.options.pageIndex = 1;
        }
        if(this.options.pageIndex>=this.options.pageSize){
            this.options.pageIndex = this.options.pageSize;
        }
        this.creatPage(this.options.pageIndex);
        localStorage.page = this.options.pageIndex;
    }
    Pager.prototype = {
        creatCount: function () {
            var creatC =
                '<div class="pageCount">' +
                '<span class="pageBack">上一页</span>' +
                '<div class="pageList"></div>' +
                '<span class="pageNext">下一页</span>' +
                '<span class="pageZ">共'+ this.options.pageSize +'页</span>'+
                '<span class="pageT">到第<input class="pageInput" type="text">页</span>' +
                '<span class="pageJump">确定</span>'
                + '</div>';
            $('body').append(creatC);
            var that = this;
            $('.pageBack').click(function () {   //上一页
                var localPage = localStorage.page;
                localPage--;
                if(localPage<1){
                    localPage = 1;
                    $(this).css({
                        'background':'#fff'
                    });
                    return false
                }
                localStorage.page = localPage;
                that.creatPage(localPage)
            })  ;
            $('.pageNext').click(function () {    //下一页
                var localPage = localStorage.page;
                localPage++;
                if(localPage > that.options.pageSize){
                    localPage = that.options.pageSize;
                    $(this).css({
                        'background':'#fff'
                    });
                    return false
                }
                localStorage.page = localPage;
                that.creatPage(localPage)
            }) ;
            $('.pageJump').click(function () {      //跳转
                var pageInput = $('.pageInput').val();
                if (!/^[1-9]\d*$/.test(pageInput)) {
                    alert('请输入正整数');
                    $('.pageInput').val('');
                }else{
                    if(pageInput > that.options.pageSize){
                        pageInput = that.options.pageSize;
                        localStorage.page = pageInput;
                        that.creatPage(pageInput);
                        $('.pageInput').val(that.options.pageSize);
                        return false
                    }
                    if(pageInput < 1){
                        pageInput = 1;
                        localStorage.page = pageInput;
                        that.creatPage(pageInput) ;
                        return false
                    }
                    localStorage.page = pageInput;
                    that.creatPage(pageInput) ;
                }
            }) ;
            $(".pageInput").on("keypress", function (event) {
                if (event.keyCode == "13") {
                    $(this).blur();
                    var pageInput = $('.pageInput').val();
                    if (!/^[1-9]\d*$/.test(pageInput)) {
                        alert('请输入正整数');
                        $('.pageInput').val('');
                    }else{
                        if(pageInput > that.options.pageSize){
                            pageInput = that.options.pageSize;
                            localStorage.page = pageInput;
                            that.creatPage(pageInput);
                            $('.pageInput').val(that.options.pageSize);
                            return false
                        }
                        if(pageInput < 1){
                            pageInput = 1;
                            localStorage.page = pageInput;
                            that.creatPage(pageInput) ;
                            return false
                        }
                        localStorage.page = pageInput;
                        that.creatPage(pageInput) ;
                    }
                }
            });
        },
        creatPage: function (pageClick) {
            var pageSize = this.options.pageSize;
            var pageList = $('.pageList');
            var pageClick = Number(pageClick);
            var span;
            var pageLast;
            var spanOne;
            var that = this;
            pageList.empty();
            if (pageSize <= 7) {
                for (var i = 1; i < pageSize + 1; i++) {
                    span = '<span class="pageL">' + i + '</span>';
                    pageList.append(span)
                }
            } else {
                if (pageClick < 5) {
                    if (pageSize >= 0 && pageSize <= 7) {
                        for (var i = 1; i < pageSize + 1; i++) {
                            span = '<span class="pageL">' + i + '</span>';
                            pageList.append(span)
                        }
                    } else if (pageSize > 7) {
                        for (var i = 1; i < 7; i++) {
                            span = '<span class="pageL">' + i + '</span>';
                            pageList.append(span)
                        }
                        pageLast = '<a>...</a><span class="pageL">' + pageSize + '</span>';
                        pageList.append(pageLast);
                    }
                } else if (pageClick >= 5 && pageClick <= pageSize - 3) {
                    spanOne = '<span class="pageL">1</span><a>...</a>';
                    pageList.append(spanOne);
                    for (var i = pageClick - 2; i < pageClick + 3; i++) {
                        span = '<span class="pageL">' + i + '</span>';
                        pageList.append(span)
                    }
                    pageLast = '<a>...</a><span class="pageL">' + pageSize + '</span>';
                    pageList.append(pageLast);
                } else if (pageClick > pageSize - 3 && pageClick <= pageSize - 2) {
                    spanOne = '<span class="pageL">1</span><a>...</a>';
                    pageList.append(spanOne);
                    for (var i = pageClick - 3; i < pageClick + 2; i++) {
                        span = '<span class="pageL">' + i + '</span>';
                        pageList.append(span)
                    }
                    pageLast = '<span class="pageL">' + pageSize + '</span>';
                    pageList.append(pageLast);
                } else if (pageClick == pageSize - 1) {
                    spanOne = '<span class="pageL">1</span><a>...</a>';
                    pageList.append(spanOne);
                    for (var i = pageClick - 4; i < pageClick + 2; i++) {
                        span = '<span class="pageL">' + i + '</span>';
                        pageList.append(span)
                    }
                } else if (pageClick == pageSize) {
                    spanOne = '<span class="pageL">1</span><a>...</a>';
                    pageList.append(spanOne);
                    for (var i = pageSize - 5; i < pageSize + 1; i++) {
                        span = '<span class="pageL">' + i + '</span>';
                        pageList.append(span)
                    }
                }
            }
            $('.pageL').css({
                "background": that.options.background,
                "color": that.options.color
            });
            for (var i = 0; i < $('.pageL').length; i++) {
                if ($('.pageL').eq(i).html() == pageClick) {
                    $('.pageL').eq(i).css({
                        "background": that.options.backgroundC,
                        "color": that.options.colorC
                    }).siblings('span').css({
                        "background": that.options.background,
                        "color": that.options.color
                    });
                }
            }
            $('.pageBack').css({
                "background": that.options.background,
            });
            $('.pageNext').css({
                "background": that.options.background,
            });
            $('.pageJump').css({
                "background": that.options.background,
            });
            $('.pageL').on('click', function () {
                that.creatPage($(this).html());
                localStorage.page = $(this).html();
                for (var i = 0; i < $('.pageL').length; i++) {
                    if ($('.pageL').eq(i).html() == $(this).html()) {
                        $('.pageL').eq(i).css({
                            "background": that.options.backgroundC,
                            "color": that.options.colorC
                        }).siblings('span').css({
                            "background": that.options.background,
                            "color": that.options.color
                        });
                    }
                }
            });
            if(pageClick <= 1){
                $('.pageBack').css({
                    'background':'#fff'
                });
                return false
            }
            if(pageClick >= this.options.pageSize){
                $('.pageNext').css({
                    'background':'#fff'
                });
                return false
            }
        }
    };
    $.fn.pager = function (options) {
        options = $.extend(defaults, options || {});
        return new Pager(options);
    }
})(jQuery);

